<template>
  <HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过">
    <ul class="goods-list">
      <li v-for="item in goods" :key="item.id">
        <RouterLink to="/">
          <!-- <img :src="item.picture" alt="" /> -->
          <img v-imglazy="item.picture" alt="" />
          <p class="name">{{ item.title }}</p>
          <p class="desc">{{ item.alt }}</p>
        </RouterLink>
      </li>
    </ul>
    <template #right>
      <xtx-more></xtx-more>
    </template>
  </HomePanel>
</template>

<script>
import HomePanel from './home-panel'
import { findHot } from '@/api/home'
import { ref } from 'vue'
// import { useIntersectionObserver } from '@vueuse/core'
import { useObserve } from '@/hooks'
export default {
  name: 'HomeHot',
  components: { HomePanel },
  // vue3写法
  setup () {
    const goods = ref([])
    const getGoods = async () => {
      const data = await findHot()
      goods.value = data.result
    }
    // 无脑发请求（干掉）
    // getGoods()
    // 监控元素或组件是否进入可视区
    /**
     * 步骤：
     * 1. 通过ref获取要监控的元素或组件
     * 2. 调用useIntersectionObserver(要监控的元素DOM对象或组件实例, 监控的回调函数)=》多次触发
     *    说明：监控函数执行完，可以解构出stop方法=》停止监控
     */
    const { target } = useObserve(getGoods)
    return { goods, target }
  }
  // vue2写法
  // data () {
  //   return {
  //     goods: []
  //   }
  // },
  // async created () {
  //   const data = await findHot()
  //   this.goods = data.result
  // }
}
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
